// src/main/java/com/example/daibanshiwu/controller/LoginController.java
package com.example.daibanshiwu.controller;

import com.example.daibanshiwu.Service.LoginService;
import com.example.daibanshiwu.pojo.Result;
import com.example.daibanshiwu.pojo.TUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    private final LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }

    @PostMapping("/login")
    // 【核心修改】: @RequestBody 直接将JSON映射为 TUser 对象
    public Result login(@RequestBody TUser loginUser) {
        try {
            TUser authenticatedUser = loginService.login(loginUser);
            // 登录成功，为了安全，不将密码返回给前端
            //authenticatedUser.setPassword(null);
            return Result.success(authenticatedUser);
        } catch (RuntimeException e) {
            // 捕获Service层抛出的异常
            return Result.error(e.getMessage());
        }
    }
}
